import requests
import pymysql

f=open("password.txt","r")
password=f.read()
f.close()
password=password.replace("\n","")
conn = pymysql.connect(host='localhost', user='root',
                       password=password, database='teamfight_wiki', port=3306)
cursor = conn.cursor()
base_url = "https://game.gtimg.cn/images/lol/act/img/tft/js/"
url = base_url + "chess.js"
count = 0
response = requests.get(url)
data = response.json()
for i in data["data"]:
    sql = f"""select hero_id from hero where hero_name="{i["title"]+" "+i["displayName"]}";"""
    cursor.execute(sql)
    result = cursor.fetchall()
    hero_id=result[0][0]

    trammel_ids=[]
    sql = f"""select trammel_id from trammel where trammel_name="{i["races"]}";"""
    cursor.execute(sql)
    result = cursor.fetchall()
    if len(result)>0:
        trammel_ids.append(result[0][0])
    for job in i["jobs"].split(","):
        sql = f"""select trammel_id from trammel where trammel_name="{job}";"""
        cursor.execute(sql)
        result = cursor.fetchall()
        if len(result) > 0:
            trammel_ids.append(result[0][0])
    for trammel_id in trammel_ids:
        sql = f"""insert into hero_trammel values({hero_id},{trammel_id})"""
        cursor.execute(sql)
conn.commit()
